package com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.stat;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.PowerChangeMonitorConfig;
import lombok.Data;
import lombok.Setter;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.IntFunction;

/**
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/24
 */
@Data
public class PowerChangePerformanceDetailReq {
    /**
     * 区域id
     */
    @NotNull(message = "区域id不能为空")
    private Integer accountId;
    /**
     * 车辆编码
     */
    private String userCode;

    /**
     * 运维人员id
     */
    private Integer accountUserId;

    /**
     * 开始时间
     */
    @NotNull(message = "开始时间不能为空")
    private LocalDate startTime;
    /**
     * 结束时间
     */
    @NotNull(message = "结束时间不能为空")
    private LocalDate endTime;
    /**
     * 车辆类型
     * 0-电单车 1-自行车
     */
    @NotNull(message = "车辆类型不能为空")
    private Integer machineType;
    /**
     * 运营分区id列表
     */
    private List<Integer> regionIds;
    /**
     * 换电区间
     * 0-馈电  -1~0
     * 1-低电  1%~10%
     * 2-中电 11%~20%
     * 3-高电 21%~100%
     */
    private Integer powerRange;
    /**
     * 审核类型
     * 0-人工
     * 1-系统
     */
    private Integer auditType;
    /**
     * 页码
     */
    @NotNull(message = "页码不能为空")
    private Integer pageNo;
    /**
     * 每页数量
     */
    @NotNull(message = "每页数量不能为空")
    private Integer pageSize;

    @JsonIgnore
    @JSONField(deserialize = false)
    private String powerRangeSqlConcat;

    /**
     * 导出可选字段列表，和返回值的字段名一致<br>
     * 传null默认导出所有字段，传空列表则不导出任何可选字段<br>
     * 可选值: socDiff, firstOrderTime, orderCountAfterChange, parkName, performanceDetail
     */
    private List<String> exportFields;

    private static final String BETWEEN_TEMPLATE = " BETWEEN %d AND %d";

    public void buildPowerRangeSqlCondition(IntFunction<PowerChangeMonitorConfig> selectConfigFn) {
        if (Objects.isNull(this.powerRange)) {
            return;
        }

        PowerChangeMonitorConfig monitorConfig = selectConfig(selectConfigFn);
        switch (this.powerRange) {
            case 0:
                this.powerRangeSqlConcat = String.format(" < %d", monitorConfig.getPowerLowMin());
                return;
            case 1:
                this.powerRangeSqlConcat = String.format(BETWEEN_TEMPLATE, monitorConfig.getPowerLowMin(), monitorConfig.getPowerLowMax());
                return;
            case 2:
                this.powerRangeSqlConcat = String.format(BETWEEN_TEMPLATE, monitorConfig.getPowerMiddleMin(), monitorConfig.getPowerMiddleMax());
                return;
            case 3:
                this.powerRangeSqlConcat = String.format(" >= %d", monitorConfig.getPowerHighMin());
                return;
            default:
                throw new BaseException("未匹配到换电区间设置");
        }
    }

    private PowerChangeMonitorConfig selectConfig(IntFunction<PowerChangeMonitorConfig> selectConfigFn) {
        return Optional.ofNullable(selectConfigFn.apply(this.accountId))
                .orElseGet(() -> new PowerChangeMonitorConfig().putDefaultValue(this.accountId));
    }
}
